class Solution
{
public:
    vector<int> findAnagrams(string s, string p)
    {
        vector<int> res;
        int hash_p[26] = { 0 };
        int hash_s[26] = { 0 };

        int right = 0;
        int left = 0;
        int n = s.size();
        int m = p.size();

        int count = 0;

        for (auto it : p)
        {
            hash_p[it - 'a']++;
        }

        while (right < n)
        {
            hash_s[s[right] - 'a']++;
            if (hash_s[s[right] - 'a'] <= hash_p[s[right] - 'a'])count++;

            if (right - left + 1 > m)
            {
                if (hash_s[s[left] - 'a'] <= hash_p[s[left] - 'a'])count--;
                hash_s[s[left] - 'a']--;
                left++;
            }

            if (count == m)
            {
                res.push_back(left);
            }
            right++;
        }

        return res;
    }
};